package cn.changeforyou.web.blog.controller;

import cn.changeforyou.web.blog.po.Article;
import cn.changeforyou.web.blog.service.ArticleService;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;


/**
 * 博客前台处理器
 * @author zhyu
 */
@IocBean
@At("/article")
@Ok("json:full")
public class ArticleFrontController {

	@Inject
	private
	ArticleService articleService;

	/**
	 * 分页查询
	 */
	@At(value = "/list" , methods="OPTIONS")
	@GET
	public NutMap getArticlePage(@Param("..") Pager pager) {
		QueryResult queryResult = articleService.getArticlePage(pager);
		return new NutMap("status", 0).setv("data", queryResult.getList()).setv("total", queryResult.getPager().getRecordCount());
	}

	/**
	 * 获取单篇文章
	 */
	@At("/?")
	@GET
	public NutMap getById(int id) {
		Article article = articleService.getById(id);
		return new NutMap("status", 0).setv("data", article);
	}

	/**
	 * 搜索文章
	 */
	@At
	@GET
	public NutMap search(long t, String keywords, @Param("..") Pager pager) {
		QueryResult queryResult = articleService.search(t, keywords, pager);
		return new NutMap("status", 0).setv("data", queryResult.getList()).setv("total", queryResult.getPager().getRecordCount());
	}

	/**
	 * 获取全部标签
	 */
	@At
	@GET
	public NutMap searchTag() {
		return new NutMap("status", 0).setv("data", articleService.searchTag());
	}

}
